{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<div style=\"color:#777777;background-color:#ffffff;font-size:12px;text-align:right;\">\n",
    "\tprepared by Abuzer Yakaryilmaz (QuSoft@Riga) | November 14, 2018\n",
    "</div>\n",
    "<table><tr><td><i> I have some macros here. If there is a problem with displaying mathematical formulas, please run me to load these macros.</i></td></td></table>\n",
    "$ \\newcommand{\\bra}[1]{\\langle #1|} $\n",
    "$ \\newcommand{\\ket}[1]{|#1\\rangle} $\n",
    "$ \\newcommand{\\braket}[2]{\\langle #1|#2\\rangle} $\n",
    "$ \\newcommand{\\inner}[2]{\\langle #1,#2\\rangle} $\n",
    "$ \\newcommand{\\biginner}[2]{\\left\\langle #1,#2\\right\\rangle} $\n",
    "$ \\newcommand{\\mymatrix}[2]{\\left( \\begin{array}{#1} #2\\end{array} \\right)} $\n",
    "$ \\newcommand{\\myvector}[1]{\\mymatrix{c}{#1}} $\n",
    "$ \\newcommand{\\myrvector}[1]{\\mymatrix{r}{#1}} $\n",
    "$ \\newcommand{\\mypar}[1]{\\left( #1 \\right)} $\n",
    "$ \\newcommand{\\mybigpar}[1]{ \\Big( #1 \\Big)} $\n",
    "$ \\newcommand{\\sqrttwo}{\\frac{1}{\\sqrt{2}}} $\n",
    "$ \\newcommand{\\dsqrttwo}{\\dfrac{1}{\\sqrt{2}}} $\n",
    "$ \\newcommand{\\onehalf}{\\frac{1}{2}} $\n",
    "$ \\newcommand{\\donehalf}{\\dfrac{1}{2}} $\n",
    "$ \\newcommand{\\hadamard}{ \\mymatrix{rr}{ \\sqrttwo & \\sqrttwo \\\\ \\sqrttwo & -\\sqrttwo }} $\n",
    "$ \\newcommand{\\vzero}{\\myvector{1\\\\0}} $\n",
    "$ \\newcommand{\\vone}{\\myvector{0\\\\1}} $\n",
    "$ \\newcommand{\\vhadamardzero}{\\myvector{ \\sqrttwo \\\\  \\sqrttwo } } $\n",
    "$ \\newcommand{\\vhadamardone}{ \\myrvector{ \\sqrttwo \\\\ -\\sqrttwo } } $\n",
    "$ \\newcommand{\\myarray}[2]{ \\begin{array}{#1}#2\\end{array}} $\n",
    "$ \\newcommand{\\X}{ \\mymatrix{cc}{0 & 1 \\\\ 1 & 0}  } $\n",
    "$ \\newcommand{\\Z}{ \\mymatrix{rr}{1 & 0 \\\\ 0 & -1}  } $\n",
    "$ \\newcommand{\\Htwo}{ \\mymatrix{rrrr}{ \\frac{1}{2} & \\frac{1}{2} & \\frac{1}{2} & \\frac{1}{2} \\\\ \\frac{1}{2} & -\\frac{1}{2} & \\frac{1}{2} & -\\frac{1}{2} \\\\ \\frac{1}{2} & \\frac{1}{2} & -\\frac{1}{2} & -\\frac{1}{2} \\\\ \\frac{1}{2} & -\\frac{1}{2} & -\\frac{1}{2} & \\frac{1}{2} } } $\n",
    "$ \\newcommand{\\CNOT}{ \\mymatrix{cccc}{1 & 0 & 0 & 0 \\\\ 0 & 1 & 0 & 0 \\\\ 0 & 0 & 0 & 1 \\\\ 0 & 0 & 1 & 0} } $\n",
    "$ \\newcommand{\\norm}[1]{ \\left\\lVert #1 \\right\\rVert } $"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h2> <font color=\"blue\"> Solutions for </font> Vectors: One Dimensional List </h2>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a id=\"task1\"></a>\n",
    "<h3> Task 1 </h3>\n",
    "\n",
    "Create two 7-dimensional vectors $u$ and $ v $ as a list in python having entries randomly picked between $-10$ and $10$. \n",
    "\n",
    "Print their entries."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3>Solution</h3>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from random import randrange\n",
    "\n",
    "dimension = 7\n",
    "\n",
    "# create u and v as empty list \n",
    "u = []\n",
    "v = []\n",
    "\n",
    "for i in range(dimension):\n",
    "    u.append(randrange(-10,11)) # add a randomly picked number to the list u\n",
    "    v.append(randrange(-10,11)) # add a randomly picked number to the list v\n",
    "\n",
    "# print both lists\n",
    "print(\"u is\",u)\n",
    "print(\"v is\",v)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a id=\"task2\"></a>\n",
    "<h3> Task 2 </h3>\n",
    "\n",
    "By using the same vectors, find the vector $  (3  u-2  v) $ and print its entries. Here $ 3u $ and $ 2v $ means $u$ and $v$ are multiplied by $3$ and $2$, respectively."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3>Solution</h3>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# let's create a result list \n",
    "\n",
    "# the first method\n",
    "result=[]\n",
    "\n",
    "# fill it with zeros\n",
    "for i in range(dimension): \n",
    "    result.append(0)\n",
    "\n",
    "print(\"result is initialized by the first method to \",result)\n",
    "\n",
    "# the second method\n",
    "# alternative and shorter solution for creating a list with zeros\n",
    "result = [0] * 7 \n",
    "\n",
    "print(\"result is initialized by the second method to\",result)\n",
    "\n",
    "# let's calculate 3u-2v\n",
    "for i in range(dimension):\n",
    "    result[i] = 3 * u[i] - 2 * v[i]\n",
    "\n",
    "# print all lists\n",
    "print(\"u is\",u)\n",
    "print(\"v is\",v)\n",
    "print(\"3u-2v is\",result)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a id=\"task3\"></a>\n",
    "<h3> Task 3 </h3>\n",
    "\n",
    "Let $ u = \\myrvector{1 \\\\ -2 \\\\ -4 \\\\ 2} $ be a four dimensional vector.\n",
    "\n",
    "Verify that $ \\norm{4 u} = 4 \\cdot \\norm{u} $ in python. \n",
    "\n",
    "Remark that $ 4u $ is another vector obtained from $ u $ by multiplying it with 4."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3>Solution</h3>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "u = [1,-2,-4,2]\n",
    "\n",
    "fouru=[4,-8,-16,8]\n",
    "\n",
    "len_u = 0\n",
    "len_fouru = 0\n",
    "for i in range(len(u)):\n",
    "    len_u = len_u + u[i]**2 # adding square of each value \n",
    "    len_fouru = len_fouru + fouru[i]**2 # adding sqaure of each value\n",
    "\n",
    "len_u = len_u ** 0.5 # taking square root of the summation\n",
    "len_fouru = len_fouru ** 0.5 # taking square root of the summation\n",
    "\n",
    "# print the lengths\n",
    "print(\"length of u is\",len_u)\n",
    "print(\"4 * length of u is\",4 * len_u)\n",
    "print(\"length of 4u is\",len_fouru)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a id=\"task4\"></a>\n",
    "<h3> Task 4 </h3>\n",
    "\n",
    "Let $ u = \\myrvector{1 \\\\ -2 \\\\ -4 \\\\ 2} $ be a four dimensional vector.\n",
    "\n",
    "Randomly pick a number $r$ from $ \\left\\{ \\dfrac{1}{10}, \\dfrac{2}{10}, \\cdots, \\dfrac{9}{10} \\right\\} $.\n",
    "\n",
    "Find the vector $(-r)\\cdot u$ and then its length."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3>Solution</h3>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from random import randrange\n",
    "\n",
    "u = [1,-2,-4,2]\n",
    "\n",
    "print(\"u is\",u)\n",
    "\n",
    "r = randrange(9) # r is a number in {0,...,8}\n",
    "r = r + 1 # r is a number in {1,...,9}\n",
    "r = r/10 # r is a number in {1/10,...,9/10}\n",
    "\n",
    "print()\n",
    "print(\"r is\",r)\n",
    "\n",
    "newu=[]\n",
    "\n",
    "for i in range(len(u)):\n",
    "    newu.append(-1*r*u[i])\n",
    "\n",
    "print()   \n",
    "print(\"-ru is\",newu)\n",
    "print()\n",
    "\n",
    "length = 0\n",
    "\n",
    "for i in range(len(newu)):\n",
    "    length = length + newu[i]**2 # adding square of each number\n",
    "    print(newu[i],\"->[square]->\",newu[i]**2)\n",
    "    \n",
    "print()    \n",
    "print(\"the summation of squares is\",length)    \n",
    "length = length**0.5 # taking square root\n",
    "print(\"the length of\",newu,\"is\",length)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Remark that:\n",
    "\n",
    "The length of $ u $ is 5.\n",
    "\n",
    "The length of $ (-r)u $ will be $ 5r $.  "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
